home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / freopen.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  2KB  |  66 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include "lib.h"
  8.  
  9. extern int __mint;
  10.  
  11. /*
  12.  * re-coded,  foobared code deleted
  13.  *
  14.  *    ++jrb
  15.  */
  16. FILE *
  17. freopen(filename, mode, fp)
  18.     const char *filename, *mode;
  19.     FILE *fp;
  20. {
  21.     unsigned int f;
  22.     
  23.     if (fp == NULL)
  24.     return NULL;
  25.     
  26.     f = fp->_flag;
  27.     if ((f & (_IORW | _IOREAD | _IOWRT)) != 0)
  28.     { /* file not closed, close it */
  29. #if 0
  30.         if (fflush(fp) != 0) return NULL;            /* flush err */
  31.         if (close(fp->_file) != 0) return NULL;        /* close err */
  32. #else
  33.     fflush(fp);                /* ANSI says ignore errors */
  34.     if (__mint || !(f & _IODEV))        /* leave tty's alone */
  35.       close(fp->_file);
  36. #endif
  37.     }
  38.     /* save buffer discipline and setbuf settings, and _IOWRT just for
  39.        determinining line buffering after the next _fopen_i */
  40.     f &= (_IOFBF | _IOLBF | _IONBF | _IOMYBUF | _IOWRT);
  41.  
  42.     /* open the new file according to mode */
  43.     if ((fp = _fopen_i(filename, mode, fp)) == NULL)
  44.     return NULL;
  45.     if (fp->_flag & _IODEV)
  46.     { /* we are re-opening to a tty */
  47.     if ((f & _IOFBF) && (f & _IOWRT) && (f & _IOMYBUF))
  48.     {   /* was a FBF & WRT & !setvbuff'ed  */
  49.         /* reset to line buffering */
  50.         f &= ~_IOFBF;
  51.         f |=  _IOLBF;
  52.     }
  53.     }
  54.     f &= ~_IOWRT; /* get rid of saved _IOWRT flag */
  55.     
  56.     /* put buffering and discipline flags in new fp->_flag */
  57.     fp->_flag &= ~(_IONBF | _IOLBF | _IOFBF | _IOMYBUF);
  58.     fp->_flag |= f;
  59.     
  60.     if (fp->_base == NULL)
  61.       /* get new buffer if file was closed */
  62.       _getbuf (fp);
  63.  
  64.     return fp;
  65. }
  66.